package com.example.demo.task;

import com.example.demo.Utils.TimeUtils;
import com.example.demo.config.Variable;
import com.example.demo.mapper.CaculateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ScheduleTask {

    @Autowired
    CaculateMapper caculateMapper;
    float times = Variable.times;
    float reduceBudgetMoney = Variable.CountBudgetMoney;
    //每周日凌晨一点执行
    @Scheduled(cron = "0 0 23 ? * SUN")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        String nowTime = TimeUtils.getNowTime();
        String thisWeekStartTime = TimeUtils.getThisWeekStart();
        float thisWeekBuySum = caculateMapper.queryThisWeekBuySum(nowTime.substring(8),thisWeekStartTime.substring(8));

        reduceBudgetMoney -= thisWeekBuySum;
        times--;
    }

    public static void main(String[] args) {
        new ScheduleTask().configureTasks();
    }
}